﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Skywave.Reporting
{
    public enum HtmlReportFieldType
    {
        String = 1,
        Html = 2,
        RelativeUrl = 3,
        HtmlTable = 4,
        DateTime = 5,
        Repeater = 6,
    }

    public abstract class HtmlReportField
    {
        internal abstract string GetContent(HtmlReportFieldGetContentArgs e);
        internal abstract void Create_Internal(string toParse);

        private object _Parent = null;
        /// <summary>
        /// ...
        /// </summary>
        public object Parent
        {
            get { return _Parent; }
            set { _Parent = value; }
        }

        private List<HtmlReportParsedField> _ParsedFields;
        /// <summary>
        /// ...
        /// </summary>
        public List<HtmlReportParsedField> ParsedFields
        {
            get { return _ParsedFields; }
            set { _ParsedFields = value; }
        }

        private string _ParsedContent;
        /// <summary>
        /// ...
        /// </summary>
        public string ParsedContent
        {
            get { return _ParsedContent; }
            set { _ParsedContent = value; }
        }

        public static HtmlReportField Create(string toParse, object parent)
        {
            HtmlReportField r = null;
            HtmlReportFieldType rType;
            List<HtmlReportParsedField> r_ParsedFields = null;
            //
            string toParse_Head, toParse_Content, toParse_End;
            int i1;
            if (toParse.EndsWith("</skywave:ReportField>"))
            {
                i1 = toParse.IndexOf(">");
                toParse_Head = toParse.Substring(0, i1 + 1);
                toParse_Content = toParse.Substring(toParse.IndexOf(">") + 1);
                toParse_End = "</skywave:ReportField>";
                toParse_Content = toParse_Content.Substring(0, toParse_Content.Length - toParse_End.Length);
            }
            else
            {
                toParse_Head = toParse;
                toParse_Content = "";
                toParse_End = "";
            }
            //
            if (!string.IsNullOrEmpty(toParse_Content))
                r_ParsedFields = HtmlReport.ParseFields(ref toParse_Content, null);
            //
            toParse = toParse_Head + toParse_Content + toParse_End;
            // Parse the properties and settings from the string
            using (System.IO.StringReader sr1 = new System.IO.StringReader(HtmlReport.Parsing_FixToParse(toParse)))
            {
                System.Xml.XmlReader xmlR1 = HtmlReport.Parsing_GetXmlReader(sr1);
                bool isStarted = false;
                string s1, s2, sKey;
                //
                while (xmlR1.Read())
                {
                    if (xmlR1.Depth == 0 && xmlR1.IsStartElement("ReportField", "http://skywavetech.ir/skywave/reporting"))
                    {
                        rType = (HtmlReportFieldType)int.Parse(xmlR1.GetAttribute("Type"));
                        switch (rType)
                        {
                            case HtmlReportFieldType.String:
                                r = new HtmlReportFieldString();
                                break;
                            case HtmlReportFieldType.Html:
                                r = new HtmlReportFieldHtml();
                                break;
                            case HtmlReportFieldType.RelativeUrl:
                                r = new HtmlReportFieldRelativeUrl();
                                break;
                            case HtmlReportFieldType.HtmlTable:
                                r = new HtmlReportFieldHtmlTable();
                                break;
                            case HtmlReportFieldType.DateTime:
                                r = new HtmlReportFieldDateTime();
                                break;
                            case HtmlReportFieldType.Repeater:
                                r = new HtmlReportFieldRepeater();
                                break;
                            default:
                                break;
                        }
                        r.Type = rType;
                        r.Parent = parent;
                        r.Id = xmlR1.GetAttribute("Id");
                        r.BindingPath = xmlR1.GetAttribute("BindingPath");
                        if (r_ParsedFields != null)
                            foreach (var fe1 in r_ParsedFields)
                                fe1.Field.Parent = r;
                        r.ParsedFields = r_ParsedFields;
                        r.ParsedContent = toParse_Content;
                        isStarted = true;
                    }
                    //
                    if (isStarted && xmlR1.Depth == 1)
                    {
                        if (xmlR1.IsStartElement("Option"))
                        {
                            if (r.Options == null)
                                r.Options = new Dictionary<string, string>();
                            sKey = xmlR1.GetAttribute("Key");
                            s1 = xmlR1.GetAttribute("Value");
                            if (string.IsNullOrEmpty(s1))
                            {
                                s1 = xmlR1.ReadInnerXml();
                                s2 = "";
                                using (System.IO.StringReader sr2 = new System.IO.StringReader(s1))
                                {
                                    while ((s1 = sr2.ReadLine()) != null)
                                    {
                                        s1 = s1.Trim();
                                        if (!string.IsNullOrEmpty(s1))
                                            s2 += s1;
                                    }
                                }
                                s1 = s2;
                            }
                            r.Options.Add(sKey, s1);
                        }
                    }
                }
            }
            //
            if (r != null)
                r.Create_Internal(toParse);
            //
            return r;
        }

        private string _Id;
        /// <summary>
        /// ...
        /// </summary>
        public string Id
        {
            get { return _Id; }
            set { _Id = value; }
        }

        private string _Text;
        /// <summary>
        /// ...
        /// </summary>
        public string Text
        {
            get { return _Text; }
            set { _Text = value; }
        }

        public string BindingPath { get; set; }

        private HtmlReportFieldType _Type;
        /// <summary>
        /// ...
        /// </summary>
        public HtmlReportFieldType Type
        {
            get { return _Type; }
            set { _Type = value; }
        }

        private Dictionary<string, string> _Options = new Dictionary<string, string>();
        /// <summary>
        /// ...
        /// </summary>
        public Dictionary<string, string> Options
        {
            get { return _Options; }
            set { _Options = value; }
        }

        public string GetOptionValue(string key)
        {
            string r = "";
            if (Options != null)
            {
                if (Options.ContainsKey(key))
                    r = Options[key];
            }
            return r;
        }
    }
}
